package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actmonthfund;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.MonthFundInfo;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 成长基金
 * @author Dell
 *
 */
public class ActiveModule_超值月基金 extends PlayerModule {
	private static final Logger logger = Logger.create(ActiveModule_超值月基金.class);

	//缓存过期时间
	private static long expire = 1;
	private static TimeUnit expireTimeUnit = TimeUnit.DAYS;

	//bean -------------------------------
	public static final int acttype = 16;
	
	private MonthFundInfo monthfund1;
	private MonthFundInfo monthfund2;
	public MonthFundInfo getMonthfund1() {
		return monthfund1;
	}
	public void setMonthfund1(MonthFundInfo monthfund1) {
		this.monthfund1 = monthfund1;
	}
	public MonthFundInfo getMonthfund2() {
		return monthfund2;
	}
	public void setMonthfund2(MonthFundInfo monthfund2) {
		this.monthfund2 = monthfund2;
	}
	
	//func ---------------------------------
	
	public static final int mftype1 = 1;	//128元档
	public static final int mftype2 = 2;	//328元档
	
	private static ActiveModule_超值月基金 create(String playerCode) {
		ActiveModule_超值月基金 am = new ActiveModule_超值月基金();
		am.setPlayerCode(playerCode);
		am.set();
		return am;
	}
	
	private static MonthFundInfo createMonthFund(int type) {
		if(type != mftype1 && type != mftype2) {
			return null;
		}
		
		DateTime now = DateTime.now();

		MonthFundInfo mf = new MonthFundInfo();
		mf.setDrawtime(null);
		mf.setUpdatetime(now);
		mf.setBuyTime(now);
		mf.setLogindays(1);
		mf.setType(type);
		return mf;
	}
	
	public MonthFundInfo getMonthFundByType(int type) {
		if(type == mftype1) {
			return getMonthfund1();
		}
		
		if(type == mftype2) {
			return getMonthfund2();
		}
		
		return null;
	}
	
	public void setMonthFundByType(int type, MonthFundInfo mf) {
		if(type == mftype1) {
			setMonthfund1(mf);
		}
		
		if(type == mftype2) {
			setMonthfund2(mf);
		}
	}
	
	public int canBuy(int type) {
		if(!running()) {
			return DrawStatus.cannotget.getStatus();
		}
		
		MonthFundInfo mFund = getMonthFundByType(type);
		if(mFund == null) {
			return DrawStatus.canget.getStatus();
		}
		
		if(mFund.getBuyTime() == null) {
			return DrawStatus.canget.getStatus();
		}
		
		if(mFund.getLogindays() >= 30 && mFund.getLogindays() == mFund.getGetids().size()) {
			return DrawStatus.canget.getStatus();
		}
		
		return DrawStatus.hadget.getStatus();
	}
	
	public void handlebuy(int goodsid) {
		int type = getTyprOrGoodsid(0, goodsid);
		MonthFundInfo mf = createMonthFund(type);
		if(mf == null) {
			return;
		}
		
		if(type == mftype1) {
			setMonthfund1(mf);
			set();
		}
		
		if(type == mftype2) {
			setMonthfund2(mf);
			set();
		}
	}
	
	/**
	 * 获取type或goodsid
	 * @param type
	 * @param goodsid
	 * @return
	 */
	public int getTyprOrGoodsid(int type, int goodsid) {
		Map<String, actmonthfund> amfmap = ActDataCenter.getAllData(actmonthfund.class, getPlayerCode());
		for(actmonthfund data : amfmap.values()) {
			if(type == 0) {	//获取goodsid
				if(goodsid == data.getChargeid()) {
					return data.getType();
				}
			}
			
			if(goodsid == 0) {	//获取type
				if(type == data.getType()) {
					return data.getChargeid();
				}
			}
		}
		
		return 0;
	}
	
	// redis ---------------------------------------------------
	
	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
		    String key1 = key(getPlayerCode());
			setv(key1, this);

			check();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_超值月基金 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_超值月基金 module = getv(key(playerCode));

			//新用户不需要去查数据库
			if (module == null && !isNewPlayer) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_超值月基金.class);
				if(module != null){module.set();}//同步到redis
			}
			if (module == null) {
				module = create(playerCode);
			}
			if(module == null){return null;}

			module.check();
			module.update();

			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private void update() {

		Map<String, actmonthfund> datamap = ActDataCenter.getAllData(actmonthfund.class, getPlayerCode());

		// 月基金领完了并且非当天领取，重置
		if (getMonthfund1() != null) {
			MonthFundInfo mf = getMonthfund1();
			if (mf.getDrawtime() != null) {
				int num = 0;
				for (actmonthfund data : datamap.values()) {
					if (data.getType() == mf.getType()) {
						num++;
					}
				}

				int days = ActiveUtil.days(mf.getDrawtime(), DateTime.now());

				if (mf.getGetids().size() >= num && ActiveUtil.days(mf.getDrawtime(), DateTime.now()) >= 1) {
					reset(mf.getType());
				}
			}
		}

		if (getMonthfund2() != null) {
			MonthFundInfo mf = getMonthfund2();
			if (mf.getDrawtime() != null) {
				int num = 0;
				for (actmonthfund data : datamap.values()) {
					if (data.getType() == mf.getType()) {
						num++;
					}
				}

				if (mf.getGetids().size() >= num && ActiveUtil.days(mf.getDrawtime(), DateTime.now()) >= 1) {
					reset(mf.getType());
				}
			}
		}

		if (getMonthfund1() != null) {
			MonthFundInfo mf = getMonthfund1();
			DateTime updatetime = mf.getUpdatetime();
			if(updatetime == null) {
				mf.setLogindays(1);
				set();
			}else {
				int days = ActiveUtil.days(updatetime, DateTime.now());
				if (days >= 1) {
					mf.setUpdatetime(DateTime.now());
					int logindays = mf.getLogindays();
					logindays++;
					mf.setLogindays(logindays);
					set();
				}				
			}
		}

		if (getMonthfund2() != null) {
			MonthFundInfo mf = getMonthfund2();
			DateTime updatetime = mf.getUpdatetime();
			if(updatetime == null) {
				mf.setLogindays(1);
				set();
			}else {
				int days = ActiveUtil.days(updatetime, DateTime.now());
				if (days >= 1) {
					mf.setUpdatetime(DateTime.now());
					int logindays = mf.getLogindays();
					logindays++;
					mf.setLogindays(logindays);
					set();
				}				
			}
		}
	}

	private void reset(int type) {
//		MonthFundInfo mf = getMonthFundByType(type);
		setMonthFundByType(type, null);
		set();
	}
	
	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);

		if(checkmf(mftype1) || checkmf(mftype2)) {
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_超值月基金奖励, true);
			return;
		}
		new TipHandle().updateTip(getPlayerCode(), TipType.活动_超值月基金奖励, false);
	}
	
	/**
	 * 根据type检查对应月基金红点
	 * @param type
	 * @return
	 */
	private boolean checkmf(int type) {
		MonthFundInfo mf = getMonthFundByType(type);
		if(mf == null) {
			return false;
		}
		
		if(mf.getBuyTime() != null) {
			int logindays = mf.getLogindays();
			int getids = mf.getGetids().size();
			
			if (logindays > getids) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_超值月基金奖励, true);
				return true;
			}
		}
		return false;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_超值月基金 activeModule_超值月基金 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_超值月基金);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
